package com.hmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageResult;
import com.hmall.item.service.IItemService;
import com.hmall.item.synchronization.MqConstants;
import com.hmall.search.pojo.ItemDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://localhost:9200")));
    @Override
    public PageResult pagesearch(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<Item> itemPage=itemMapper.pageQuery();
        PageResult pageResult=new PageResult();
        pageResult.setTotal(itemPage.getTotal());
        pageResult.setList(itemPage.getResult());
        return pageResult;
    }
    @Override
    public Item selectByid(Integer id) {
        return itemMapper.selectById(id);
    }

    @Override
    public void change(Integer id, Integer status) {
        //判断状态为1加入es，状态为2删除es
        if(status.equals(1)){
          rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,id);
        }else if(status.equals(2)){
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DELETE_KEY,id);
        }
        itemMapper.change(id,status);
    }

    @Override
    public void redece(Long itemId, Long num) {
        Item item = itemMapper.selectById(itemId);
        Integer stock=item.getStock()-num.intValue();
        itemMapper.changenum(itemId,stock);
    }

    public void deleteById(Long id) {
        try {
            // 1.准备Request
            DeleteRequest request = new DeleteRequest("item", id.toString());
            // 2.发送请求
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void insertById(Long id) {
        try {
            // 0.根据id查询酒店数据
            Item item=selectByid(id.intValue());
            // 转换为文档类型
            ItemDoc itemDoc = new ItemDoc(item);

            // 1.准备Request对象
            IndexRequest request = new IndexRequest("item").id(item.getId().toString());
            // 2.准备Json文档
            request.source(JSON.toJSONString(itemDoc), XContentType.JSON);

            // 3.发送请求
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
